package com.apes.autoparts.parts.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.autoparts.parts.model.Part;
import com.apes.autoparts.parts.model.SubGroup;
import com.apes.autoparts.parts.repository.PartDao;
import com.apes.autoparts.parts.repository.PartRepository;
import com.apes.autoparts.parts.repository.SubGroupRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service("dataMatchService")
public class DataMatchService extends DomainService {

    @Autowired
    private QueryVinService queryVinService;

    @Autowired
    private QueryMainGroupService queryMainGroupService;

    @Autowired
    private PartDao partDao;
    @Autowired
    private PartRepository partRepository;
    @Autowired
    private SubGroupRepository subGroupRepository;
    @Autowired
    private QuerySubGroupService querySubGroupService;

    @Autowired
    private QueryPartsService queryPartsService;

    /***
     * vin 解析
     */
    public JSONObject parseVin(SimpleRequest request) {
        return queryVinService.queryVin(request);
    }

    public JSONObject checkVinData(SimpleRequest request) {
        return queryVinService.checkVinData(request);
    }


    /***
     * 原始目录
     */


    /***
     * 汽车车身目录
     */


    /***
     * oem目录
     */


    /***
     * 通过车架号查询主组数据
     * @return
     */
    public JSONArray parseMainGroup(SimpleRequest request) {

        return queryMainGroupService.queryMainGroup(request);
    }

    /***
     * 通过主组查询子组数据
     * @return
     */
    public JSONArray parseSubGroup(SimpleRequest request) {
        return querySubGroupService.querySubGroup(request);
    }

    /***
     * 通过子组查询热点图数据
     * @return
     */
    public JSONObject parseImgPoint(SimpleRequest request) {
        return querySubGroupService.queryImgPoint(request);
    }

    /***
     * 通过子组查询配件数据
     * @return
     */
    public List<Object> parseParts(SimpleRequest request) {
        return JSONObject.parseArray(queryPartsService.queryParts(request).toString(), Object.class);
    }


    /***
     * 模糊查询配件
     * @return
     */
    public JSONObject fuzzyQueryOfParts(SimpleRequest request) {
        return queryPartsService.fuzzyQueryOfParts(request);
    }

    /***
     * 模糊查询配件
     * @return
     */
    public JSONObject fuzzyOfParts(SimpleRequest request) {
        return queryPartsService.fuzzyOfParts(request);
    }


    /**
     * 通过OE号获取品牌件信息
     * @param request
     * @return
     */
    public JSONObject queryBrandParts(SimpleRequest request) {
        return queryPartsService.queryBrandParts(request);
    }


    /**
     * 配件筛选
     * @param request
     * @return
     */
    public JSONObject queryPartsSimple(SimpleRequest request) {
        return queryPartsService.queryPartsSimple(request);
    }


    public JSONObject searchParts(SimpleRequest request) {
        return queryPartsService.searchParts(request);
    }


    public JSONObject searchPartsByVinKeywords(SimpleRequest request) {
        return queryPartsService.searchPartsByVinKeywords(request);
    }

    /***
     * 方位图数据(树形结构)
     */
    public JSONArray queryOrientationMap(SimpleRequest request) {

        JSONObject param = request.getJO();

        // 查主目录
        JSONArray menuArray = partDao.queryMainOrientation();

        // 循环查子组
        menuArray = menuArray.stream().map(obj -> {

            JSONObject item = (JSONObject) obj;

            JSONArray subMenus = partDao.querySubOrientation(item.getString("MENU_NAME"));

            subMenus = subMenus.stream().map(json -> {
                // 循环查配件名称

                JSONObject subMenu = (JSONObject) json;
                JSONArray pjmcArray = partDao.queryPJMC(item.getString("MENU_NAME"), subMenu.getString("MENU_NAME"), param.getString("selectorId"));

                subMenu.put("Children", pjmcArray);
                return subMenu;
            }).filter(json -> {
                // 去除无目录的子组
                JSONObject subMenu = (JSONObject) json;
                return subMenu.getJSONArray("Children").size() > 0;
            }).collect(Collectors.toCollection(JSONArray::new));

            item.put("Children", subMenus);

            return item;
        }).collect(Collectors.toCollection(JSONArray::new));

        return menuArray;
    }

    /***
     * 获取对应的 oe 码
     */
    public List<Part> queryPartByPJMC(SimpleRequest request) {

        JSONObject param = request.getJO();

        JSONArray ids = partDao.queryPartByPJMC(param.getString("PJMCID"), param.getString("selectorId")).stream()
                .map(obj -> {
                    JSONObject item = (JSONObject) obj;
                    return item.getString("ID");
                }).collect(Collectors.toCollection(JSONArray::new));

        List<Part> partList = partRepository.findAll(
                JpaDsl.toCriteria(
                        "id", "in", ids
                )
        );

        return partList;
    }

    /***
     * 通过 oe 码获取对应的上级目录
     */

    public SubGroup querySubGroupByPartCode(SimpleRequest request) {

        SubGroup subGroup = subGroupRepository.findOne((String) request.get("subGroupId"));

        return subGroup;
    }

    /**
     * 数据中心维护的厂牌白名单
     * @param request
     * @return
     */
    public JSONObject searchZPT_PBFW(SimpleRequest request) {
        return SpringManager.getBean(PartDao.class).searchZPTPBFW(request.getJO());
    }



}
